#include <bits/stdc++.h>
using namespace std;
#define int long long

int dp[32][2][2], n, m;

inline int dfs(int i, int l1, int l2) {
	if (i == -1) return 1;
	if (dp[i][l1][l2] != -1) return dp[i][l1][l2];
	int nd = (n >> i) & 1, md = (m >> i) & 1, ans = 0;
	for (int c1 = 0; c1 <= max(l1, nd); ++c1)
		for (int c2 = c1; c2 <= max(l2, md); ++c2)
			ans += dfs(i - 1, l1||(c1!=nd), l2||(c2!=md));
	return dp[i][l1][l2] = ans;
}

int solve(int x, int y) {
	n = x;
	m = y;
	memset(dp, 255, sizeof(dp));
	return dfs(30, 0, 0);
}
signed main() {
	freopen("comb.in", "r", stdin); freopen("comb.out", "w", stdout);
	ios::sync_with_stdio(0); cin.tie(0); 
	int q;
	cin >> q;
	while (q--) {
		int l, r; cin >> l >> r;
		cout << solve(r, r) - solve(r, l - 1) - solve(l - 1, r) + solve(l - 1, l - 1) << "\n";
	}
	return 0;
}
